package com.cdu.ai_intelligent_teaching_and_training_system.service;

import com.cdu.ai_intelligent_teaching_and_training_system.entity.Resource;
import com.cdu.ai_intelligent_teaching_and_training_system.mapper.ResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ResourceService {
    
    @Autowired
    private ResourceMapper resourceMapper;
    
    public List<Resource> findAll() {
        return resourceMapper.selectAll();
    }
    
    public Optional<Resource> findById(Long id) {
        return Optional.ofNullable(resourceMapper.selectById(id));
    }
    
    public List<Resource> findByCourseId(Long courseId) {
        return resourceMapper.selectByCourseId(courseId);
    }
    
    public List<Resource> findByType(String type) {
        return resourceMapper.selectByType(type);
    }
    
    // 需要在Mapper.xml中自定义模糊查询
    public List<Resource> findByTitleContaining(String title) {
        return resourceMapper.selectByTitleLike("%" + title + "%");
    }
    
    public Resource save(Resource resource) {
        if (resource.getResourceId() == null) {
            resourceMapper.insert(resource);
        } else {
            resourceMapper.update(resource);
        }
        return resource;
    }
    
    public void deleteById(Long id) {
        resourceMapper.deleteById(id);
    }
} 